package gaileenchess;

import java.util.LinkedList;

abstract public class Player
{
    public final Common.Side side; // the colour of the current player
    private Board board;
    private LinkedList<Piece> pieceList;
    private Piece king;
    
    abstract public Piece.Type promote(); //pawn promotion
    
    public Player(Common.Side s, Board b)
    {
        this.side = s;
        this.board = b;
        setPieceList();
    }
    
    public void reset(Board b)
    {
        this.board = b;
        setPieceList();
    }
    
    public Piece getKing()
    {
        return this.king;
    }
    
    public LinkedList<Piece> getPieceList()
    {
        return this.pieceList;
    }

    public void setPieceList(LinkedList<Piece> l)
    {
        this.pieceList=l;
    }
    private void setPieceList()
    {
        Piece[][] pos = board.getPos();
        this.pieceList = new LinkedList<Piece>();
        
        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
            {
                if(pos[x][y] != null && pos[x][y].getSide() == this.side)
                {
                    this.pieceList.addFirst(pos[x][y]);
                    
                    if(pos[x][y].getType() == Piece.Type.KING)
                    {
                        this.king = pos[x][y];
                    }
                }
            }
        }
    }

    abstract Command getCommand(); // interface for getting a generic command-line or GUI command from a human or computer player
    
    // return an enum corresponding to which colour the piece is
    public Common.Side getSide()
    {
        return this.side;
    }
    
    // return a string corresponding to which colour the piece is
    public String getSideString()
    {
        if (this.side == Common.Side.WHITE)
        {
            return "White";
        }
        
        return "Black";
    }
    
    public boolean confirm()
    {
        // by default just return true, only HumanPlayer should override this
        return true;
    }
    
    
    
    public Piece[][] getSetup()
    {
        // by default just return null, only HumanPlayer should override this
        return null;
    }
    
    public LinkedList<Board.Position.Move> getValidMoves(boolean check, int player)//TODO: check parameter is no longer necessary
    {
        LinkedList<Board.Position.Move> ret = new LinkedList<Board.Position.Move>();
        
        for(Piece p : this.pieceList)
        {
            p.getValidMoves(this.side, this.board, ret, check, player);
        }
        
        return ret;
    }
    
    abstract public Player newP(Board board);
}
